package org.cybzacg.convert.listener.impl;

import org.cybzacg.convert.context.ConversionContext;
import org.cybzacg.convert.listener.ConversionListener;
import org.cybzacg.util.LogUtils;

/**
 * 日志监听器
 * 记录转换过程中的日志信息
 */
public class LoggingListener extends ConversionListener.Adapter {
    private final boolean logCacheEvents;
    private final boolean logValidationEvents;

    public LoggingListener() {
        this(true, true);
    }

    public LoggingListener(boolean logCacheEvents, boolean logValidationEvents) {
        this.logCacheEvents = logCacheEvents;
        this.logValidationEvents = logValidationEvents;
    }

    @Override
    public void onConversionStart(ConversionContext context, Object source) {
        LogUtils.logConversionStart(context, source);
    }

    @Override
    public void onConversionSuccess(ConversionContext context, Object source, Object result) {
        LogUtils.logConversionSuccess(context, source, result);
    }

    @Override
    public void onConversionFailure(ConversionContext context, Object source, Exception exception) {
        LogUtils.logConversionFailure(context, source, exception);
    }

    @Override
    public void onCacheHit(ConversionContext context, Object source, Object cachedResult) {
        if (logCacheEvents) {
            LogUtils.logCacheHit(context, source, cachedResult);
        }
    }

    @Override
    public void onCacheMiss(ConversionContext context, Object source) {
        if (logCacheEvents) {
            LogUtils.logCacheMiss(context, source);
        }
    }

    @Override
    public void onValidationSuccess(ConversionContext context, Object value) {
        if (logValidationEvents) {
            LogUtils.logValidationSuccess(value, "DefaultValidator");
        }
    }

    @Override
    public void onValidationFailure(ConversionContext context, Object value, Exception exception) {
        if (logValidationEvents) {
            LogUtils.logValidationFailure(value, "DefaultValidator", exception.getMessage());
        }
    }
}
